<template>
  <el-form :inline="true" :model="activityQuery" class="demo-form-inline" :rules="activityRules">
    <!--负责人-->
    <el-form-item label="负责人">
      <el-select
          v-model="activityQuery.ownerId"
          placeholder="请选择负责人"
          clearable
          @click="loadOwner"
      >
        <!--负责人下拉列表-->
        <el-option
            v-for="item in ownerOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
      </el-select>
    </el-form-item>

    <!--活动名称-->
    <el-form-item label="活动名称">
      <el-input v-model="activityQuery.name" placeholder="请输入活动名称" clearable />
    </el-form-item>

    <!--活动时间-->
    <el-form-item label="活动时间" class="time">
      <el-date-picker
          v-model="activityQuery.activityTime"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </el-form-item>

    <!--活动预算-->
    <el-form-item label="活动预算" prop="cost">
      <el-input v-model="activityQuery.cost" placeholder="请输入活动预算" clearable/>
    </el-form-item>

    <!--创建时间-->
    <el-form-item label="创建时间" class="createTime">
      <el-date-picker
          v-model="activityQuery.createTime"
          type="datetime"
          placeholder="请选择创建时间"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </el-form-item>

    <!--搜索/重置按钮-->
    <el-form-item>
      <el-button type="primary" @click="onSearch" plain>搜 索</el-button>
      <el-button type="danger" plain @click="onReset">重 置</el-button>
    </el-form-item>

    <!--录入市场活动/批量删除按钮-->
    <div>
      <el-form-item>
        <el-button type="primary" @click="add" plain>录入市场活动</el-button>
        <el-button type="danger" plain>批量删除</el-button>
      </el-form-item>
    </div>

    <!--活动信息表单-->
    <el-table
        ref="multipleTableRef"
        :data="activityList"
        row-key="id"
        style="width: 100%"
        @selection-change="handleSelectionChange">

      <el-table-column type="selection" :selectable="selectable"/>
      <el-table-column label="序号" type="index" width="80px"></el-table-column>
      <el-table-column property="ownerDO.name" label="负责人"/>
      <el-table-column property="name" label="活动名称" />
      <el-table-column property="startTime" label="开始时间" />
      <el-table-column property="endTime" label="结束时间" />
      <el-table-column property="cost" label="活动预算"/>
      <el-table-column property="createTime" label="创建时间"/>
      <el-table-column label="操作" show-overflow-tooltip>
        <template #default="scope">
          <el-button type="primary" @click="view(scope.row.id)">详情</el-button>
          <el-button type="success" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background layout="prev, pager, next"
        :page-size="pageSize"
        :total="total"
        @prev-click="toPage"
        @next-click="toPage"
        @current-change="toPage"
    >
    </el-pagination>
  </el-form>


</template>

<script>
import {doGet} from "../http/httpRequest.js";

export default {
  name: "ActivityView",
  data(){
    return {
      activityQuery: {},
      activityList: [{
        ownerDO: {}
      }],
      // 每页显示记录条数
      pageSize: 0,
      // 总记录条数
      total: 0,
      // 负责人下拉列表
      ownerOptions: [{}],

      // 验证规则
      activityRules: {
        cost:[
          { pattern: /^[0-9]+(\.[0-9]{2})?$/ ,message: '活动预算必须是正整数',trigger: 'blur'}
        ]
      }
    }
  },

  // 在页面渲染时就执行
  mounted() {
    this.getData(1);
  },

  methods:{
    // 活动分页查询
    getData(current){
      let startTime='';
      let endTime=''

      for (let key in this.activityQuery.activityTime){
        if (key==='0'){
          startTime=this.activityQuery.activityTime[key]
        }
        if (key==='1'){
          endTime=this.activityQuery.activityTime[key]
        }
      }

      doGet('/api/activities',{
        // 页数
        current: current,

        // 传递参数
        ownerId: this.activityQuery.ownerId,
        name: this.activityQuery.name,
        startTime: startTime,
        endTime: endTime,
        cost: this.activityQuery.cost,
        createTime: this.activityQuery.createTime
      }).then((resp)=>{
        if (resp.data.code===200){
          this.activityList= resp.data.data.list
          this.pageSize = resp.data.data.pageSize
          this.total = resp.data.data.total
        }
      })
    },

    // 分页函数
    toPage(current){
      this.getData(current)
    },

    // 加载负责人
    loadOwner(){
      doGet('/api/owner',{}).then((resp)=>{
        if (resp.data.code===200){
          this.ownerOptions=resp.data.data;
        }
      })
    },

    // 搜索
    onSearch(){
      this.getData(1)
    },

  onReset(){
    this.activityQuery={}
  },

    // 录入市场活动
    add(){
      this.$router.push('/dashboard/activity/add')
    },

    // 编辑市场活动
    edit(id){
      this.$router.push('/dashboard/activity/edit/'+id)
    },

    // 市场活动详情
    view(id){
      this.$router.push('/dashboard/activity/'+id)
    }
  }
}
</script>

<style scoped>
.el-form-item {
  width: 230px;
}
.time {
  width: 470px;
}
.el-pagination {
  margin-top: 10px;
}
</style>